package com.sren.initializrstart.job;

import com.sren.initializrstart.service.CityDataService;
import com.sren.initializrstart.service.WeatherDataService;
import com.sren.initializrstart.vo.City;
import java.util.List;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.util.CollectionUtils;

/**
 * @author sren
 * @create 2018-12-09 下午10:44
 **/

@Slf4j
public class WeatherDataSyncJob extends QuartzJobBean {

    @Autowired
    private CityDataService cityDataService;

    @Autowired
    private WeatherDataService weatherDataService;
    @Override
    protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        try {
            List<City> cities = cityDataService.listCity();
            if (!CollectionUtils.isEmpty(cities)) {
                log.info("天气数据同步开始");
                cities.stream()
                        .forEach(e->{

                            weatherDataService.syncDataByCityId(e.getCityId());
                            weatherDataService.syncDataByCityName(e.getCityName());
                        });
                log.info("天气数据同步完成");
            }
        } catch (Exception e) {
            log.error("天气数据同步错误", e);
        }
    }
}
